﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

/// <summary>
/// Summary description for Common1
/// </summary>
public class Common1
{
	public Common1()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public static void ResizeImage(System.Drawing.Image image, int newwidthimg, string filepath)
    {

        //int newwidthimg = image.Size.Width;
        float AspectRatio = (float)image.Size.Width / (float)image.Size.Height;
        int newHeight = Convert.ToInt32(newwidthimg / AspectRatio);
        Bitmap thumbnailBitmap = new Bitmap(newwidthimg, newHeight);
        Graphics thumbnailGraph = Graphics.FromImage(thumbnailBitmap);
        thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality;
        thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality;
        thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
        var imageRectangle = new Rectangle(0, 0, newwidthimg, newHeight);
        thumbnailGraph.DrawImage(image, imageRectangle);
        thumbnailBitmap.Save(filepath, ImageFormat.Jpeg);
        thumbnailGraph.Dispose();
        thumbnailBitmap.Dispose();
        image.Dispose();

    }

    public static string getImageUrl(string imageLocation)
    {
        

        // Here is the page address
        String pa = HttpContext.Current.Request.Url.AbsoluteUri;

        // Take the page name    
        String pn = HttpContext.Current.Request.Url.LocalPath;

        // Here is the server address    
        String sa = pa.Replace(pn, "");

        // Take the physical location of the page    
        String pl = HttpContext.Current.Request.PhysicalPath;

        // Replace the backslash with slash in your path    
        pl = pl.Replace("\\", "/");
        imageLocation = imageLocation.Replace("\\", "/");

        // Root path     
        String rp = pl.Replace(pn, "");

        // Take out same path    
        String final = imageLocation.Replace(rp, "");

        // So your picture's address is    
        String path = sa + final;

        return path;
    }

    public static string ResolveServerUrl(string serverUrl, bool forceHttps)
    {
        if (serverUrl.IndexOf("://") > -1)
            return serverUrl;

        string newUrl = serverUrl;
        Uri originalUri = System.Web.HttpContext.Current.Request.Url;
        newUrl = (forceHttps ? "https" : originalUri.Scheme) +
            "://" + originalUri.Authority + newUrl;
        return newUrl;
    }

    public static string getHost()
    {
        string sPath = System.Web.HttpContext.Current.Request.Url.AbsoluteUri;
        string sPage = sPath.Substring(sPath.LastIndexOf("/") + 1);
        return sPath.Replace(sPage,"");
    }
}